package app

import (
	"bili-server/app/log"
	"bili-server/app/web"
	"embed"
	"fmt"
	"github.com/judwhite/go-svc"
	"github.com/urfave/cli/v2"
	"github.com/zing-dev/soft-version/soft"
	"os"
)

type App struct {
	web *web.Web
	v   []byte
	www embed.FS
}

func (a *App) Init(env svc.Environment) error {
	log.L.Info(fmt.Sprintf("is win service? %v", env.IsWindowsService()))
	return nil
}

func (a *App) Start() error {
	log.L.Info("Starting...")
	a.web = new(web.Web)
	a.web.Run()
	return nil
}

func (a *App) Stop() error {
	log.L.Info("Stopping...")
	a.web.Shutdown()
	log.L.Info("Stopped...")
	return nil
}

func New(v []byte, www embed.FS) *App {
	return &App{
		v:   v,
		www: www,
	}
}

func (a *App) Run() {
	c := soft.NewCli(&cli.App{
		Name:        "账户平台",
		Usage:       "账户平台控制台运行软件",
		Description: "账户平台控制台运行软件",
		HelpName:    "app",
		Action: func(c *cli.Context) error {
			return svc.Run(a)
		},
		Commands: []*cli.Command{
			{
				Name:  "web",
				Usage: "运行web应用",
				Action: func(c *cli.Context) error {
					return svc.Run(a)
				},
			},
		},
	}, a.v)
	err := c.Run(os.Args)
	if err != nil {
		log.L.Fatal(err)
	}
}
